What is es6-error?
The es6-error package provides a simple and effective way to create custom error classes in JavaScript using ES6 syntax. It extends the native Error class, making it easier to create and manage custom error types with proper stack traces.
What are es6-error's main functionalities?
Creating Custom Error Classes
This feature allows you to create custom error classes by extending the ExtendableError class provided by es6-error. The custom error class can then be used to throw and catch specific error types, with proper stack traces.
class CustomError extends ExtendableError {
constructor(message) {
super(message);
this.name = 'CustomError';
}
}
try {
throw new CustomError('This is a custom error');
} catch (err) {
console.error(err.name); // CustomError
console.error(err.message); // This is a custom error
console.error(err.stack); // Stack trace
}
Handling Custom Errors
This feature demonstrates how to handle custom errors by creating a specific error class (NotFoundError) and using it in a function. When the error is thrown, it can be caught and handled specifically based on its type.
class NotFoundError extends ExtendableError {
constructor(resource) {
super(`${resource} not found`);
this.name = 'NotFoundError';
}
}
function findResource(resource) {
if (!resource) {
throw new NotFoundError('Resource');
}
return resource;
}
try {
findResource(null);
} catch (err) {
if (err instanceof NotFoundError) {
console.error('Specific handling for NotFoundError');
} else {
console.error('General error handling');
}
}
Other packages similar to es6-error
custom-error-generator
The custom-error-generator package provides a way to create custom error classes with additional properties and methods. It is similar to es6-error but offers more flexibility in defining custom error properties and methods.
extendable-error-class
The extendable-error-class package allows you to create custom error classes by extending the native Error class. It is similar to es6-error but focuses on providing a minimalistic approach to creating custom error classes.
create-error-class
The create-error-class package provides a simple way to create custom error classes with additional properties. It is similar to es6-error but offers a more concise syntax for defining custom error classes.
es6-error
An easily-extendable error class for use with ES6 classes (or ES5, if you so
choose).
Tested in Node 4.0, Chrome, and Firefox.
Why?
I made this because I wanted to be able to extend Error for inheritance and type
checking, but can never remember to add
Error.captureStackTrace(this, this.constructor.name)
to the constructor or how
to get the proper name to print from console.log
.
ES6 Usage
import ExtendableError from 'es6-error';
class MyError extends ExtendableError {
constructor(message = 'Default message') {
super(message);
}
}
export default MyError;
ES5 Usage
var util = require('util');
var ExtendableError = require('es6-error');
function MyError(message) {
message = message || 'Default message';
ExtendableError.call(this, message);
}
util.inherits(MyError, ExtendableError);
module.exports = MyError;
Known Issues
- Uglification can obscure error class names (#31)
Todo
- Better browser compatibility
- Browser tests